package com.ywh.floating

import android.app.Activity
import android.content.Context
import android.util.Log
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.core.view.contains
import com.ywh.floating.ext.flog
import com.ywh.floating.lifecycle.IFloatingAttach
import com.ywh.floating.view.FloatingView

/**
 * Create by yangwenhao on 2022/8/5
 */
object FloatingManager : IFloatingAttach {
    private var config: FloatingConfig? = null
    private var floatingView: FloatingView? = null
    private var statusBar = 0

    override fun attach(activity: Activity) {
        val config = config ?: return
        if (!config.canAttachClass(activity::class)) {
            return
        }
        flog(this::class.java, "attach to ${activity.javaClass.name}")
        //悬浮窗展示了就return
        if (floatingView != null) {
            return
        }
        val floatingView = getView(activity)
        val decorView = activity.window.decorView as FrameLayout

        decorView.post {
            val decorViewWidth = decorView.measuredWidth
            val decorViewHeight = decorView.measuredHeight
            Log.e("DecorView", "decorViewWidth:${decorViewWidth},decorViewHeight:${decorViewHeight}")
            floatingView?.maxSize(decorViewWidth, decorViewHeight)
        }
        floatingView?.apply {
            if (decorView.contains(this)) {
                flog(this::class.java, "decorView contains floatingView")
            }
            if (this.parent is ViewGroup) {
                val parent = parent as ViewGroup
                parent.removeView(this)
            }
            val layoutParams = FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT)
            layoutParams.topMargin = config.initialY
            layoutParams.leftMargin = config.initialX
            decorView.addView(this, layoutParams)
        }
    }

    override fun detach() {
        flog(this::class.java, "detach from last Activity")
        if (floatingView?.parent !is ViewGroup) return
        (floatingView?.parent as ViewGroup).removeView(floatingView)
    }

    override fun close() {
        config = null
        detach()
        floatingView = null
    }

    override fun setFloatingConfig(config: FloatingConfig?) {
        this.config = config
    }

    private fun getView(context: Context): FloatingView? {
        val config = config ?: return null
        if (config.mLayoutId == 0) return null
        if (floatingView == null) {
            floatingView = FloatingView(context, null, config).apply {
                moveUpdateListener = { leftMargin, topMargin ->
                    config.initialX = leftMargin
                    config.initialY = topMargin
                    flog(this@FloatingManager::class.java, "move--leftMargin:${leftMargin},topMargin:${topMargin}")
                    val lp = layoutParams as? FrameLayout.LayoutParams
                    lp?.leftMargin = leftMargin
                    lp?.topMargin = topMargin
                    layoutParams = lp
                }
            }
        }
        return floatingView
    }
}